/**
 * localStroage 封装
 */
import config from '../config'
export default {
  /**
   * 设置
   * @param {*} key key
   * @param {*} value value
   */
  setItem(key, value) {
    // 取
    let storage = this.getStorage();
    storage[key] = value;
    window.localStorage.setItem(config.namespace, JSON.stringify(storage));
  },
  /**
   * 根据key获取
   * @param {*} key key
   * @returns string
   */
  getItem(key) {
    return this.getStorage()[key];
  },
  /**
   * 删除localStorage中key
   */
  clearItem(key) {
    let storage = this.getStorage();
    delete storage[key];
    window.localStorage.setItem(config.namespace, JSON.stringify(storage));
  },
  /**
   * 清空localStorage
   */
  clearAll() {
    window.localStorage.clear();
  },
  /**
   * 获取localStorage中的所有内容
   * @returns 字符串
   */
  getStorage() {
    let storage = window.localStorage.getItem(config.namespace) || "{}";
    return JSON.parse(storage);
  },
};